//
//  HZGoodsSearchListData.m
//  app
//
//  Created by 刘华舟 on 15/5/20.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HZGoodsSearchListData.h"

static const NSInteger kGoodsSearchListPageSize = 15;

@implementation HZGoodsSearchListData

+ (instancetype)dataWithDictionary:(NSDictionary *)dict{
    return [[HZGoodsSearchListData alloc] initDataWithDictionary:dict];
}
- (instancetype)initDataWithDictionary:(NSDictionary *)result{
    if (self = [super init]) {
        
        if ([result count]) {
            NSMutableArray *dataArray = [NSMutableArray array];
            for (NSDictionary *dict in [result arrayForKey:APIKEY_mall_result]) {
                HZGoodsModel *model = [HZGoodsModel modelWithDictionary:dict];
                [dataArray addObject:model];
            }
            
            self.goodsArray = dataArray;
            if (self.goodsArray.count < kGoodsSearchListPageSize) {
                self.noMoreData = YES;
            }else{
                self.noMoreData = NO;
            }
            
            self.lastObjectIndex = self.goodsArray.count - 1;
        }else{
            self.goodsArray = [NSMutableArray array];
            self.noMoreData = YES;
            self.lastObjectIndex = 0;
        }
        
    }
    return self;
}

-(HZGoodsModel *)fetchCellModelAtRow:(NSInteger)row{
    return [self.goodsArray objectAtIndex:row];
}

- (BOOL)addMoreDataWithArray:(NSArray *)moreDatas{
    if (moreDatas.count <= 0) {
        self.noMoreData = YES;
        return NO;
    }else{
        if (moreDatas.count < kGoodsSearchListPageSize) {
            self.noMoreData = YES;
        }

        NSMutableArray *totalArray = [NSMutableArray arrayWithArray:self.goodsArray];
        for (NSDictionary *dict in moreDatas) {
            HZGoodsModel *model = [HZGoodsModel modelWithDictionary:dict];
            [totalArray addObject:model];
        };
        self.goodsArray = totalArray;
        
        self.lastObjectIndex = self.goodsArray.count - 1;
    }
    return YES;
}

@end
